package com.zhiche.lisa.lspm.controller.api;

import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.enums.DriverCheckinStatusEnum;
import com.zhiche.lisa.core.enums.exception.DriverExceptionEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.service.driver.IDriverCheckinService;
import com.zhiche.lisa.lspm.service.driver.IDriverService;
import com.zhiche.lisa.lspm.vo.driver.DriverVO;
import com.zhiche.lisa.lspm.dao.model.driver.bo.DriverBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * api controller
 */
@RestController
@RequestMapping("/api")
@Api(value = "/api", description = "提供给第三方的接口",tags={"提供给第三方的接口"})
public class ApiController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApiController.class);

    @Autowired
    private IDriverService driverService;

    @Autowired
    private IDriverCheckinService driverCheckinService;

    /**
     * 获取所有司机
     */
    @RequestMapping(value = "/getAllDriver",method = RequestMethod.GET)
    @ApiOperation(value = "获取所有司机", notes = "获取所有司机", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<List<DriverVO>> getAllDriver(){
        LOGGER.info("ApiController.getAllDriver");
        RestfulResponse<List<DriverVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(),"success",null);
        try{
            List<DriverBO> listBO = driverService.getAllDriver();
            List<DriverVO> listVO = new ArrayList<>();
            for(DriverBO driverBO : listBO){
                DriverVO driverVO = new DriverVO();
                BeanUtils.copyProperties(driverBO,driverVO);
                listVO.add(driverVO);
            }
            result.setData(listVO);
        }catch(BaseException be){
            LOGGER.error("ApiController.getAllDriver error:{}",be.getMessage());
            result.setMessage(be.getMessage());
            result.setCode(be.getCode());
        }catch(Exception e){
            LOGGER.error("ApiController.getAllDriver error:{}",e.getMessage());
            result.setMessage("获取司机信息失败,系统异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }


    /**
     * 更新报班状态
     */
    @RequestMapping(value = "/updateCheckinStatus",method = RequestMethod.POST)
    @ApiOperation(value = "更新报班状态", notes = "更新报班状态", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<Object> updateCheckinStatus(Long checkinId,String status){
        LOGGER.info("ApiController.updateCheckinStatus param : {} {}", checkinId,status);
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(),"success",null);
        try{
            if(DriverCheckinStatusEnum.NOT_ARRANGED.getCode().equals(status) || DriverCheckinStatusEnum.ARRANGED.equals(status)){
                driverCheckinService.updateCheckinStatus(checkinId,status);
            }else{
                //状态数据传递不正确
                throw new BaseException(DriverExceptionEnum.DRIVER_CHECKIN_STATUS_ERROR.getCode(), DriverExceptionEnum.DRIVER_CHECKIN_STATUS_ERROR.getText());
            }
        }catch(BaseException be){
            LOGGER.error("ApiController.updateCheckinStatus error:{}",be.getMessage());
            result.setMessage(be.getMessage());
            result.setCode(be.getCode());
        }catch(Exception e){
            LOGGER.error("ApiController.updateCheckinStatus error:{}",e.getMessage());
            result.setMessage("更新报班信息失败,系统异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

}
